package cm.tl.pms.sys.controller;

import cm.tl.pms.common.entity.ResultMsg;
import cm.tl.pms.sys.entity.SysDept;
import cm.tl.pms.sys.service.ISysDeptService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统-组织机构管理
 *
 * @author laker
 * @since 2021-08-11
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController {
    @Autowired
    ISysDeptService sysDeptService;

    /**
     * 分页查询
     *
     * @param current
     * @param size
     * @return
     */
    @GetMapping
    public ResultMsg<Page<SysDept>> pageAll(@RequestParam(required = false, defaultValue = "1") long current,
                                            @RequestParam(required = false, defaultValue = "10") long size) {
        Page<SysDept> roadPage = new Page<>(current, size);
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        Page<SysDept> pageList = sysDeptService.page(roadPage, queryWrapper);
        return ResultMsg.successfulResult(pageList);
    }

    /**
     * 新增或者更新
     *
     * @param param
     * @return
     */
    @PostMapping
    @SaCheckPermission("dept.update")
    public ResultMsg saveOrUpdate(@RequestBody SysDept param) {
        return ResultMsg.successfulResult(sysDeptService.saveOrUpdate(param));
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @SaCheckPermission("dept.detail")
    public ResultMsg get(@PathVariable Long id) {
        return ResultMsg.successfulResult(sysDeptService.getById(id));
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("dept.delete")
    public ResultMsg delete(@PathVariable Long id) {
        return ResultMsg.successfulResult(sysDeptService.removeById(id));
    }

    /**
     * 根据批量删除ids删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/batch/{ids}")
    @SaCheckPermission("dict.delete")
    public ResultMsg batchRemove(@PathVariable Long[] ids) {
        return ResultMsg.successfulResult(sysDeptService.removeByIds(CollUtil.toList(ids)));
    }

    /**
     * 获取组织列表不分页
     *
     * @return
     */
    @GetMapping("/data")
    public ResultMsg data() {
        List<SysDept> data = sysDeptService.list(Wrappers.<SysDept>lambdaQuery()
                .eq(SysDept::getStatus, true)
                .orderByAsc(SysDept::getSort));
        return ResultMsg.successfulResult(data);
    }

    /**
     * 获取组织树
     *
     * @return
     */
    @GetMapping("/tree")
    public ResultMsg tree() {
        List<SysDept> data = sysDeptService.list(Wrappers.<SysDept>lambdaQuery()
                .eq(SysDept::getStatus, true)
                .orderByAsc(SysDept::getSort));
        return ResultMsg.successfulResult(data);
    }

}
